<?php

namespace app\test512\controller;

//vendor('aliyuncs.oss-sdk-php.autoload');
use OSS\OssClient;
use OSS\Core\OssException;
use think\Controller;

class TPOSS extends Controller
{
    public $accessKeyId = "LTAI5tMz7xKjbigSQe1DqH2R";
    public $accessKeySecret = "D12DAFFw1SM5hnUvHhfR8D0wNbv8hG";
    public $endpoint = "oss-cn-shanghai.aliyuncs.com";
    public $bucket = "yw666";

    /**
     * oss 上传图片或其他文件
     * @param $object oss存储地址
     * @param $filePath 带路径文件名
     */
    public function uploadOssFile($object, $filePath)
    {
        $object = $object . '/' . $filePath;//想要保存文件的名称
        $file = './uploads\\' . $filePath;//文件路径，必须是本地的。
        try {
            $ossClient = new OssClient($this->accessKeyId, $this->accessKeySecret, $this->endpoint);
            $data = $ossClient->uploadFile($this->bucket, $object, $file);
        } catch (OssException $e) {
            return false;
        }
        return $data;
    }

    /**
     * oss判断文件是否存在
     * @param $object 要判断的文件
     */
    public function checkFileExist($object)
    {
        try {
            $ossClient = new OssClient($this->accessKeyId, $this->accessKeySecret, $this->endpoint);
            $exist = $ossClient->doesObjectExist($this->bucket, $object);
        } catch (OssException $e) {
            return false;
        }
        return $exist;
    }

    /**
     * oss 删除多个文件
     * @param $objects array  多个文件
     * @return bool
     */
    public function deleteFiles($objects)
    {
        try {
            $ossClient = new OssClient($this->accessKeyId, $this->accessKeySecret, $this->endpoint);
            $ossClient->deleteObjects($this->bucket, $objects);
        } catch (OssException $e) {
            return false;
        }
        return true;
    }

    /**
     * 获取指定文件件下的文件列表
     * @param string $prefix 指定目录
     * @param string $nextMarker 从那个文件开始（不包含）（获取文件夹下所有文件的话，值与$prefix相同）
     * @param string $delimiter 对文件名称进行分组的一个字符。CommonPrefixes是以delimiter结尾，并有共同前缀的文件集合。
     * @param string $maxkeys 列举文件的最大个数。默认为100，最大值为1000。
     * @return bool array 返回文件列表
     */
    public function listObject($prefix = '', $nextMarker = '', $delimiter = '', $maxkeys = '')
    {
        try {
            $options = array(
                'prefix' => $prefix,
                'marker' => $nextMarker,
                'delimiter' => $delimiter,
                'max-keys' => $maxkeys,
            );
            $ossClient = new OssClient($this->accessKeyId, $this->accessKeySecret, $this->endpoint);
            $listObjectInfo = $ossClient->listObjects($this->bucket, $options);
        } catch (OssException $e) {
            return false;
        }
//		$nextMarker = $listObjectInfo->getNextMarker();
        $listObject = $listObjectInfo->getObjectList();
//		$listPrefix = $listObjectInfo->getPrefixList();

        if (!empty($listObject)) {
            $list = array();
            foreach ($listObject as $objectInfo) {
                $list[] = $objectInfo->getKey();
            }
            return $list;
        } else {
            return false;
        }
    }
}